
import { getDetail } from './feedbackDetailServ.js'
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { hashHistory } from 'dva/router'

const defultState = {
  feedbackId: "",
  detail: {},
  previewImages: []
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        if ('' + tmpModule.pathname === '' + pathname) {
          dispatch({
            type: 'updateStore', payload: { feedbackId: query.feedbackId }
          })
          dispatch({
            type: 'queryInfo', payload: {}
          })
        }
      })
    },
  },

  effects: {
    *queryInfo({ payload }, { put, call, select }) {
      try {
        let { feedbackId } = yield select(state => state[tmpModule.namespace])

        let { data, success } = yield call(getDetail, parse({ feedbackId: feedbackId }))
        if (success) {
          if (data.gender == "1") {
            data.sex = "男"
          } else {
            data.sex = "女"
          }
          if (data.imgsUrl.length > 0) {
            data.imgsUrl = data.imgsUrl.split(',');
          }
          yield put({ type: 'updateStore', payload: { detail: data } })
        }

      } catch (e) {
        console.error(e)
      }
    },
    * updateModel({ payload }, { put, select }) {
      yield put({ type: 'updateStore', payload });
    },
  },

  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
